<?php

class Pipes_Statement_WithTags
{
	private $_substitutions = array();
	private $_value = '';
	
	public function __construct($name, array $tags) {
		$this->_value = "SELECT 
			`{$name}s`.* 
		FROM 
			`{$name}s` 
			INNER JOIN `{$name}s_to_tags` USING(`{$name}_id`) 
			INNER JOIN `tags` USING(`tag_id`) 
		WHERE 
			`tags`.`name` IN (" . implode(', ', array_fill(0, count($tags), '?')) . ") 
		GROUP BY 
			`{$name}s`.`{$name}_id` 
		HAVING COUNT(`{$name}s`.`{$name}_id`) = " . count($tags);
		$this->_substitutions = $tags;
	}

	public function __get($key) {
		$return = null;
		switch ($key) {
			case 'value':
			case 'substitutions';
				$realKey = '_' . $key;
				$return = $this->$realKey;
				break;
		}
		return $this->$realKey;
	}
			
}
